package com.scalar.chiptrack.operations.dataentry.inventoryTransit.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.inventoryTransit.forms.InventoryTransitForm;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 2 Jun, 2010
 * Time: 2:57:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProgramOutTransactionSearchCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/inventoryOutTrans.do" ;

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        InventoryTransitForm invOutForm = (InventoryTransitForm) form;

        HashMap<String,String> errors = new HashMap<String,String>();
        HashMap<String,String> info   = new HashMap<String,String>();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME +"?" + request.getQueryString() );
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        invOutForm.setProcessStepId( 0 );
        invOutForm.setLocationId( 0 );
        invOutForm.setWipLotsList( null );
       try
       {
           /*request.setAttribute("lotsList",        DataEntryDAO.getAllRunningHoldLots( LOCATION_TYPE_FG , invOutForm.getProcessStepId() , invOutForm.getLocationId(), invOutForm.getLotId()  ) );*/
           request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( LOCATION_TYPE_FG ) );
           request.setAttribute("locationList",    VendorDAO.getInventoryLocationsByProcessByUserType( null, LOCATION_TYPE_FINISHED_GOODS , 0 ) );

           request.setAttribute("action", action);
       }
       catch( Exception e)
       {
          errors.put( "Unable to get WIP/Inventory Lots", null);
          e.printStackTrace();
       }
       return mapping.findForward( "programOutTrans" );
    }
}
